package com.renwei.yunlong.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.InformationBean;
import com.renwei.yunlong.bean.LoginBean;
import com.renwei.yunlong.bean.ServiceLoginBean;
import com.renwei.yunlong.http.HttpPresenter;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.ACache;
import com.zyf.fwms.commonlibrary.http.Api;
import com.zyf.fwms.commonlibrary.utils.AutoUtils;
import com.zyf.fwms.commonlibrary.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import butterknife.Bind;
import butterknife.ButterKnife;
import me.leefeng.promptlibrary.PromptButton;
import me.leefeng.promptlibrary.PromptButtonListener;
import me.leefeng.promptlibrary.PromptDialog;

import static com.renwei.yunlong.R.id.iv_office_phone;
import static com.renwei.yunlong.R.id.rl_age;
import static com.renwei.yunlong.R.id.rl_born;
import static com.renwei.yunlong.R.id.rl_certificate;
import static com.renwei.yunlong.R.id.rl_phone;

/**
 * 人员信息页面
 * <p>
 * Created by Administrator on 2017/8/23.
 */

public class InformationActivity extends BaseActivity implements View.OnClickListener, HttpTaskListener {

    @Bind(R.id.iv_back)
    ImageView ivBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;

    @Bind(rl_age)
    RelativeLayout rlAge;
    @Bind(rl_born)
    RelativeLayout rlBorn;
    @Bind(R.id.rl_department)
    RelativeLayout rlDepartment;
    @Bind(R.id.rl_grouping)
    RelativeLayout rlGrouping;
    @Bind(R.id.rl_job_number)
    RelativeLayout rlJobNumber;
    @Bind(R.id.rl_character)
    RelativeLayout rlCharacter;
    @Bind(R.id.rl_status)
    RelativeLayout rlStatus;
    @Bind(rl_phone)
    RelativeLayout rlPhone;
    @Bind(iv_office_phone)
    ImageView ivOfficePhone;
    @Bind(R.id.rl_office_phone)
    RelativeLayout rlOfficePhone;
    @Bind(R.id.rl_email)
    RelativeLayout rlEmail;
    @Bind(R.id.rl_expertise)
    RelativeLayout rlExpertise;
    @Bind(rl_certificate)
    RelativeLayout rlCertificate;

    @Bind(R.id.tv_nickname)
    TextView tvNickname;
    @Bind(R.id.tv_age)
    TextView tvAge;
    @Bind(R.id.tv_birthday)
    TextView tvBirthday;
    @Bind(R.id.tv_department_name)
    TextView tvDepartmentName;
    @Bind(R.id.tv_userGroup_name)
    TextView tvUserGroupName;
    @Bind(R.id.tv_employee_code)
    TextView tvEmployeeCode;
    @Bind(R.id.tv_role_name)
    TextView tvRoleName;
    @Bind(R.id.tv_state)
    TextView tvState;
    @Bind(R.id.tv_mobile_Phone)
    TextView tvMobilePhone;
    @Bind(R.id.tv_office_telephone)
    TextView tvOfficeTelephone;
    @Bind(R.id.tv_email)
    TextView tvEmail;
    @Bind(R.id.tv_technical_qualif_name)
    TextView tvTechnicalQualifName;
    @Bind(R.id.tv_work_number)
    TextView tvWorkNumber;
    @Bind(R.id.iv_head)
    ImageView ivHead;
    @Bind(R.id.rl_click_replace)
    RelativeLayout rlClickReplace;

    private TimePickerView pvTime;

    private ACache mCache;

    private PromptDialog promptDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_information);
        ButterKnife.bind(this);

        mCache = ACache.get(this);

        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);
        //屏幕自适配
        AutoUtils.setSize(this, false, 720, 1280);
        AutoUtils.auto(this);

        initData();
    }

    private void initData() {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);
        tvTitle.setText("人员信息");

        ivBack.setOnClickListener(this);
        rlAge.setOnClickListener(this);
        rlBorn.setOnClickListener(this);
        rlExpertise.setOnClickListener(this);
        rlCertificate.setOnClickListener(this);
        rlOfficePhone.setOnClickListener(this);
        rlClickReplace.setOnClickListener(this);

        promptDialog = new PromptDialog(this);

        LoginBean bean = (LoginBean) mCache.getAsObject("loginBean");
        ServiceLoginBean serviceBean = (ServiceLoginBean) mCache.getAsObject("serviceLoginBean");
        //mCache.put("companyType","1");
        String companyType = mCache.getAsString("companyType");
        if ("1".equals(companyType)) {
            if (bean.getRows() != null) {
                String jsonBean = "{employeeId:\"" + bean.getRows().getEmployeeId() + "\"}";
                ownerEmployeeHttp(jsonBean);
                Log.e("业主 个人信息jsonBean", jsonBean);
            }
        } else if ("2".equals(companyType)) {
            if (serviceBean.getRows() != null) {
                String jsonBean = "{employeeId:\"" + serviceBean.getRows().getEmployeeId() + "\"}";
                serviceEmployeeHttp(jsonBean);
                Log.e("服务商 个人信息jsonBean", jsonBean);
            }
        }


    }

    //业主个人信息
    private void ownerEmployeeHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(InformationActivity.this);
        hp.setUrl(Api.OWNER_EMPLOYEE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(1);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    //服务商个人信息
    private void serviceEmployeeHttp(String jsonBean) {
        HttpPresenter hp = new HttpPresenter(InformationActivity.this);
        hp.setUrl(Api.SERVICE_EMPLOYEE_URL);
        hp.setshowDialogcontent("正在加载");
        hp.setRequsetId(2);
        hp.putParam("jsonBean", jsonBean);
        hp.setCallBack(this);
        hp.post();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.rl_click_replace:
                PromptButton cancle = new PromptButton("取消", null);

                //默认两个按钮为Alert对话框，大于三个按钮的为底部SHeet形式展现
                promptDialog.showAlertSheet("", true, cancle,
                        new PromptButton("打开相册", new PromptButtonListener() {
                            @Override
                            public void onClick(PromptButton button) {

                            }
                        }), new PromptButton("拍照", new PromptButtonListener() {
                            @Override
                            public void onClick(PromptButton button) {

                            }
                        }));
                break;
            case rl_age://性别
                selectSex();
                break;
            case rl_born://生日
                selectTime();
                break;
            case R.id.rl_department:
                break;
            case R.id.rl_grouping:
                break;
            case R.id.rl_job_number:
                break;
            case R.id.rl_character:
                break;
            case R.id.rl_status:
                break;
            case R.id.rl_phone:

                break;
            case R.id.rl_office_phone:
                startActivity(new Intent(this, OfficePhoneActivity.class));
                break;
            case R.id.rl_email:
                break;
            case R.id.rl_expertise:
                startActivity(new Intent(this, ExpertiseActivity.class));
                break;
            case rl_certificate:
                startActivity(new Intent(this, CertificateActivity.class));
                break;

            default:
                break;

        }
    }

    //选择性别
    private void selectSex() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this); //定义一个AlertDialog
        String[] strarr = {"男", "女"};
        builder.setItems(strarr, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                String sex = "2";
                // 自动生成的方法存根
                if (arg1 == 0) {//男
                    sex = "1";
                } else {//女
                    sex = "2";
                }
            }
        });
        builder.show();
    }

    //日期选择
    private void selectTime() {
        //时间选择器
        pvTime = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
        //设置标题
        pvTime.setTitle("出生日期");
        //控制时间范围
        Calendar calendar = Calendar.getInstance();
        pvTime.setRange(calendar.get(Calendar.YEAR) - 40, calendar.get(Calendar.YEAR));
        pvTime.setTime(new Date());
        //设置是否循环
        pvTime.setCyclic(false);
        //设置是否可以关闭
        pvTime.setCancelable(true);
        //设置选择监听
        pvTime.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date) {
                Log.e("MainActivity", "当前选择时间：" + getTime(date));
            }
        });
        //显示
        pvTime.show();
    }

    public static String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        return format.format(date);
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                Log.e("业主返回个人信息", result);
                InformationBean bean = new Gson().fromJson(result, InformationBean.class);
                if (bean.getMessage().getCode() == 200) {//加载成功

                    Glide.with(this).load("http://106.14.20.156:8083/AppCloudServer" + bean.getRows().getAvatar()).into(ivHead);
                    tvNickname.setText(bean.getRows().getName());
                    tvWorkNumber.setText("已解决工单" + bean.getRows().getWorkOrderNumber());
                    if ("0".equals(bean.getRows().getGender())) {
                        tvAge.setText("女");
                    } else if ("1".equals(bean.getRows().getGender())) {
                        tvAge.setText("男");
                    }
                    tvBirthday.setText(bean.getRows().getBirthday());
                    tvDepartmentName.setText(bean.getRows().getDepartmentName());
                    tvUserGroupName.setText(bean.getRows().getUserGroupName());
                    tvEmployeeCode.setText(bean.getRows().getEmployeeCode());
                    tvRoleName.setText(bean.getRows().getRoleName());
                    if ("1".equals(bean.getRows().getState())) {
                        tvState.setText("正常");
                    } else if ("2".equals(bean.getRows().getState())) {
                        tvState.setText("删除");
                    }
                    tvMobilePhone.setText(bean.getRows().getMobilePhone());
                    tvOfficeTelephone.setText(bean.getRows().getOfficeTelephone());
                    tvEmail.setText(bean.getRows().getEmail());
                    tvTechnicalQualifName.setText(bean.getRows().getTechnicalQualifName());
                }
                break;
            case 2:
                Log.e("服务商返回个人信息", result);
                InformationBean serviceBean = new Gson().fromJson(result, InformationBean.class);
                if (serviceBean.getMessage().getCode() == 200) {

                    Glide.with(this).load("http://106.14.20.156:8083/AppServiceProvider" + serviceBean.getRows().getAvatar()).into(ivHead);
                    tvNickname.setText(serviceBean.getRows().getName());
                    tvWorkNumber.setText("已解决工单" + serviceBean.getRows().getWorkOrderNumber());

//                    int lock = Integer.parseInt(serviceBean.getRows().getLock());

                    if ("0".equals(serviceBean.getRows().getGender())) {
                        tvAge.setText("女");
                    } else if ("1".equals(serviceBean.getRows().getGender())) {
                        tvAge.setText("男");
                    }
                    tvBirthday.setText(serviceBean.getRows().getBirthday());
                    tvDepartmentName.setText(serviceBean.getRows().getDepartmentName());
                    tvUserGroupName.setText(serviceBean.getRows().getUserGroupName());
                    tvEmployeeCode.setText(serviceBean.getRows().getEmployeeCode());
                    tvRoleName.setText(serviceBean.getRows().getRoleName());
                    if ("1".equals(serviceBean.getRows().getState())) {
                        tvState.setText("正常");
                    } else if ("2".equals(serviceBean.getRows().getState())) {
                        tvState.setText("删除");
                    }
                    tvMobilePhone.setText(serviceBean.getRows().getMobilePhone());
                    tvOfficeTelephone.setText(serviceBean.getRows().getOfficeTelephone());
                    tvEmail.setText(serviceBean.getRows().getEmail());
                    tvTechnicalQualifName.setText(serviceBean.getRows().getTechnicalQualifName());

                }
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {

    }

    private void setLock(int lock) {
        if (lock >= 0 && lock < 0.4) {

        } else if (lock == 0.5) {

        } else if (lock > 0.6 && lock < 1) {

        } else if (lock == 1) {

        } else if (lock > 1 && lock <= 1.4) {

        } else if (lock == 1.5) {

        } else if (lock > 1.6 && lock < 2) {

        } else if (lock == 2) {

        } else if (lock > 1 && lock <= 1.4) {

        }
    }

}
